package com.example.SocialApplication.adapters;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import com.example.SocialApplication.R;
import com.example.SocialApplication.db.entity.ColorEntity;

import java.util.List;

public class ColorSpectrumAdapter extends ArrayAdapter {

    private static final int[] FROM_COLOR = new int[]{255, 255, 255};
    private static final int THRESHOLD = 2;

    private List<ColorEntity> listColors;
    private Context context;
    private boolean changeColor;
    private ImageView imageChange;

    public ColorSpectrumAdapter(Context context, int textViewResourceId, List<ColorEntity> list, ImageView image, boolean changeColor) {
        super(context, textViewResourceId, list);
        this.listColors = list;
        this.context = context;
        this.changeColor = changeColor;
        this.imageChange = image;
    }

    @Override
    public int getCount() {
        return listColors.size();
    }

    @Override
    public ColorEntity getItem(int position) {
        return listColors.get(position);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        if (view == null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.color_spectrum_item, parent, false);
        }

        LinearLayout layout = (LinearLayout) view.findViewById(R.id.background_layout);
        layout.setBackgroundColor(Color.parseColor(getItem(position).getBackground()));

        ImageView image = (ImageView) view.findViewById(R.id.imageView);
        if (imageChange != null) {
            image.setImageDrawable(imageChange.getDrawable());
        }
        if (changeColor) {
            Drawable drawable = image.getDrawable();
            image.setImageDrawable(adjust(drawable, getItem(position).getIconColor()));
        }

        return view;
    }

    private Drawable adjust(Drawable d, String color) {
        int to = Color.parseColor(color);

        //Need to copy to ensure that the bitmap is mutable.
        Bitmap src = BitmapFactory.decodeResource(context.getResources(), R.drawable.backpack); ///todo set selected image, not from R.id.drawable
//        Bitmap src = ((BitmapDrawable) d).getBitmap();
        Bitmap bitmap = src.copy(Bitmap.Config.ARGB_8888, true);
        for (int x = 0; x < bitmap.getWidth(); x++)
            for (int y = 0; y < bitmap.getHeight(); y++)
                if (match(bitmap.getPixel(x, y)))
                    bitmap.setPixel(x, y, to);

        return new BitmapDrawable(bitmap);
    }

    private boolean match(int pixel) {
        //There may be a better way to match, but I wanted to do a comparison ignoring
        //transparency, so I couldn't just do a direct integer compare.
        return Math.abs(Color.red(pixel) - FROM_COLOR[0]) < THRESHOLD &&
                Math.abs(Color.green(pixel) - FROM_COLOR[1]) < THRESHOLD &&
                Math.abs(Color.blue(pixel) - FROM_COLOR[2]) < THRESHOLD;
    }
}
